package com.bjsxt.service.impl;


import com.bjsxt.mapper.AdminMapper;
import com.bjsxt.pojo.Admin;
import com.bjsxt.service.AdminServer;
import com.bjsxt.utils.BeanMapUtils;
import com.bjsxt.utils.MapParameter;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminServerImpl implements AdminServer {

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public int create(Admin admin) {

        return adminMapper.create(admin);
    }

    //根据id进行删除
    @Override
    public int delete(Integer id) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("id",id);
        Map<String, Object> map = MapParameter.getInstance().put("id", id).getMap();
        return adminMapper.delete(map);
    }

    @Override
    public int update(Admin admin) {
//        Map<String, Object> map = BeanMapUtils.beanToMapForUpdate(admin);
//        map.put("id",admin.getId());
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(admin)).getMap();
        map.put("id",admin.getId());
        System.out.println(map.toString());
        return adminMapper.update(map);
    }

    @Override
    public int deleteBatch(String ids) {
        int flag = 0;
        List<String> list = Splitter.on(",").splitToList(ids);
        for (String s:list){
            adminMapper.delete(MapParameter.getInstance().put("id",Integer.parseInt(s)).getMap());
            flag++;
        }
        return flag;
    }

    @Override
    public List<Admin> query(Admin admin) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("id",admin.getId());
//        map.put("account",admin.getAccount());
//        map.put("password",admin.getPassword());
//        map.put("name",admin.getName());
//        map.put("phone",admin.getPhone());
//        map.put("remark",admin.getRemark());

        //使用pageHelper进行分页操作
        PageHelper.startPage(admin.getPage(),admin.getLimit());  //页数 当页总条数
        Map<String, Object> map = BeanMapUtils.beanToMap(admin);
        return adminMapper.query(map);
    }

    @Override
    public Admin detail(Admin admin) {

        return adminMapper.detail(BeanMapUtils.beanToMap(admin));
    }

    @Override
    public int count(Admin admin) {

        return adminMapper.count(BeanMapUtils.beanToMap(admin));
    }

    @Override
    public Admin login(String account, String password) {
        Map<String, Object> map = MapParameter.getInstance().put("account", account).put("password", password).getMap();
        return adminMapper.detail(map);
    }
}
